$OpenBSD: patch-src_torrent_poll_kqueue_cc,v 1.7 2015/03/24 13:35:09 dcoppa Exp $

Fix a crash when total number of connections exceeds 1024.

The number of events is used to index an array of maxOpenSockets
(file descriptors) elements. If there are fewer fds than maxEvents,
this will cause a segfault.
Limit the number of events to the number of fds with an upper limit of
maxEvents.

--- src/torrent/poll_kqueue.cc.orig	Tue May  6 03:22:57 2014
+++ src/torrent/poll_kqueue.cc	Tue Mar 24 03:43:10 2015
@@ -37,6 +37,7 @@
 #include "config.h"
 
 #include <cerrno>
+#include <cassert>
 
 #include <algorithm>
 #include <unistd.h>
@@ -121,12 +122,12 @@ PollKQueue::create(int maxOpenSockets) {
   if (fd == -1)
     return NULL;
 
-  return new PollKQueue(fd, 1024, maxOpenSockets);
+  return new PollKQueue(fd, 16384, maxOpenSockets);
 }
 
 PollKQueue::PollKQueue(int fd, int maxEvents, int maxOpenSockets) :
   m_fd(fd),
-  m_maxEvents(maxEvents),
+  m_maxEvents((maxOpenSockets < maxEvents) ? maxOpenSockets : maxEvents),
   m_waitingEvents(0),
   m_changedEvents(0),
   m_stdinEvent(NULL) {
